package com.practice.draw.customview

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import kotlin.math.PI
import kotlin.math.cos
import kotlin.math.sqrt

/**
 * Created by lgs on 2018/7/17.
 */
class DrawPieView @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private val DENSITY = resources.displayMetrics.density
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val path = Path()
    private val rectF = RectF(-300f, -300f, 300f, 300f)

    override fun onDraw(canvas: Canvas?) {

        paint.style = Paint.Style.FILL
        canvas?.save()
        //黄色
        paint.color = Color.parseColor("#fec107")
        canvas?.translate(width / 2f, height / 2f)
        canvas?.drawArc(rectF, 0f, -60f, true, paint)
        canvas?.restore()

        //红色
        canvas?.save()
        paint.color = Color.RED
        canvas?.translate(width / 2f - DENSITY * 8, height / 2f - DENSITY * 8)
        canvas?.drawArc(rectF, -60f, -120f, true, paint)
        canvas?.restore()

        //蓝色p
        canvas?.save()
        paint.color = Color.parseColor("#2196f3")
        canvas?.translate(width / 2f, height / 2f)
        canvas?.drawArc(rectF, -182f, -110f, true, paint)
        canvas?.restore()

        //绿色
        canvas?.save()
        paint.color = Color.parseColor("#029688")
        canvas?.translate(width / 2f, height / 2f)
        canvas?.drawArc(rectF, 15f, 60f, true, paint)
        canvas?.restore()

        //灰色
        canvas?.save()
        paint.color = Color.parseColor("#9c9ea4")
        canvas?.translate(width / 2f, height / 2f)
        canvas?.drawArc(rectF, 14f, -5f, true, paint)
        canvas?.restore()

        //紫色
        canvas?.save()
        paint.color = Color.parseColor("#982aaf")
        canvas?.translate(width / 2f, height / 2f)
        canvas?.drawArc(rectF, 10f, -8.5f, true, paint)
        canvas?.restore()

        //b69b45
        canvas?.save()
        paint.color = Color.parseColor("#b69b45")
        canvas?.translate(width / 2f, height / 2f)
        canvas?.drawArc(rectF, 1f, -1.5f, true, paint)
        canvas?.restore()

        /*****绘制饼图上的字*****/
        paint.reset()
        paint.color = Color.WHITE
        paint.textAlign = Paint.Align.LEFT
        paint.textSize = DENSITY * 8

        path.moveTo(-150f, -300 * cos(PI / 6).toFloat())

//        canvas?.drawText()
//        canvas?.drawTextOnPath("Lollipop", path, 100f, 100f, paint)

    }

}